package com.mew.chess.core.game.pokdeng.game.task;

import com.mew.chess.core.common.handler.ChessPushMessageUtil;
import com.mew.chess.core.common.handler.GameCommonService;
import com.mew.chess.core.game.pokdeng.game.PokDengGameManager;
import com.mew.chess.core.game.pokdeng.game.PokDengRoomManager;
import com.mew.chess.core.game.pokdeng.game.PokDengTableManager;
import com.mew.chess.core.game.pokdeng.game.common.PokDengConstantValue;
import com.mew.chess.core.game.pokdeng.game.common.PokDengLinkType;
import com.mew.chess.core.game.pokdeng.game.common.PokDengPushCMDMapType;
import com.mew.chess.core.game.pokdeng.game.core.TableUtil;
import com.mew.chess.core.game.pokdeng.game.message.PushStartBetMessage;
import com.mew.common.enm.UserType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.stream.Collectors;

/**
 * @author dylin
 * @create 2021/4/29
 */
@Component
@Slf4j
public class PokDengStartBetTask {
    @Autowired
    private GameCommonService gameCommonService;
    public boolean startBet(int roomId,int tableId){
        PokDengRoomManager roomManager=PokDengGameManager.getInstance().getController(roomId);
        PokDengTableManager table = roomManager.getTableManager(tableId);
        table.initData();
        //如果session断了  剔除session
        table.getOnlineUser().forEach((k, v) -> {
            if (null == v || v.isWeakClose()){
                table.getOnlineUser().remove(k);
            }
        });
        //如果这个人的session不在了  清人
        table.getSeatMap().values().stream().forEach(s->{
            if(null!=s.getUser()&&s.getUser().getUserType()!= UserType.Robot.code()){
                long uid=s.getUser().getUserId();
                if(null==table.getOnlineUser().get(uid)){
                    table.clearOnlineUser(uid);
                }
            }
        });

        gameCommonService.checkTMSOnline(table.getOnlineUser().values().stream().collect(Collectors.toList()),  PokDengGameManager.getInstance().getGameId(), roomId);


//        TmsServerInfo tmsServerInfo = TmsConfigInfoManager.getInstance().getServerInfo();
//        if (tmsServerInfo.getOnline() < OnlineType.Online.value()){
//            log.info("PokDeng Start TMS not online {}", tmsServerInfo.toString());
//            table.stopGame();
//            table.getOnlineUser().forEach((k, v) -> {
//                aucRedisService.offlineSession(k);
//                ChessPushMessageUtil.pushMessage(v, BasisCommandCode.OVERHAULED.cmd(), new EmptyMessage());
//            });
//        }

        if (table.getOnlineUser().isEmpty()){
            log.info("PokDeng table {} not real user is stop ", tableId);
            table.stopGame();
            return false;
        }

        //看要不要补充机器人
        TableUtil.robotDispose(roomManager,table);

        //------start game-----
        table.changeLink(PokDengLinkType.StartBet.code(), PokDengConstantValue.START_BET_TIME);
        PushStartBetMessage pushStartBetMessage=table.getPushStartBetMessage();
        pushStartBetMessage.setIno(table.getIno());
        ChessPushMessageUtil.broadcastMessage(table.getOnlineUser().values(),
                PokDengPushCMDMapType.StartBetMessage.code(), pushStartBetMessage);
        return true;
    }
}
